home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / gdata / tlslite / integration / TLSSocketServerMixIn.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  2.7 KB  |  63 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''TLS Lite + SocketServer.'''
  5. from gdata.tlslite.TLSConnection import TLSConnection
  6.  
  7. class TLSSocketServerMixIn:
  8.     '''
  9.     This class can be mixed in with any L{SocketServer.TCPServer} to
  10.     add TLS support.
  11.  
  12.     To use this class, define a new class that inherits from it and
  13.     some L{SocketServer.TCPServer} (with the mix-in first). Then
  14.     implement the handshake() method, doing some sort of server
  15.     handshake on the connection argument.  If the handshake method
  16.     returns True, the RequestHandler will be triggered.  Below is a
  17.     complete example of a threaded HTTPS server::
  18.  
  19.         from SocketServer import *
  20.         from BaseHTTPServer import *
  21.         from SimpleHTTPServer import *
  22.         from tlslite.api import *
  23.  
  24.         s = open("./serverX509Cert.pem").read()
  25.         x509 = X509()
  26.         x509.parse(s)
  27.         certChain = X509CertChain([x509])
  28.  
  29.         s = open("./serverX509Key.pem").read()
  30.         privateKey = parsePEMKey(s, private=True)
  31.  
  32.         sessionCache = SessionCache()
  33.  
  34.         class MyHTTPServer(ThreadingMixIn, TLSSocketServerMixIn,
  35.                            HTTPServer):
  36.           def handshake(self, tlsConnection):
  37.               try:
  38.                   tlsConnection.handshakeServer(certChain=certChain,
  39.                                                 privateKey=privateKey,
  40.                                                 sessionCache=sessionCache)
  41.                   tlsConnection.ignoreAbruptClose = True
  42.                   return True
  43.               except TLSError, error:
  44.                   print "Handshake failure:", str(error)
  45.                   return False
  46.  
  47.         httpd = MyHTTPServer((\'localhost\', 443), SimpleHTTPRequestHandler)
  48.         httpd.serve_forever()
  49.     '''
  50.     
  51.     def finish_request(self, sock, client_address):
  52.         tlsConnection = TLSConnection(sock)
  53.         if self.handshake(tlsConnection) == True:
  54.             self.RequestHandlerClass(tlsConnection, client_address, self)
  55.             tlsConnection.close()
  56.         
  57.  
  58.     
  59.     def handshake(self, tlsConnection):
  60.         raise NotImplementedError()
  61.  
  62.  
  63.